package com.example.libraryapp.util;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.example.libraryapp.Constant;
import com.example.libraryapp.bean.User;

/**
 * @author 伍凯铭
 * @since 2020/6/21
 */
public class LoginUtil {

    public static void login(Context context, User user) {
        SharedUtil.getInstance(context).writeShared(Constant.USER_DATA, JSON.toJSONString(user));
    }

    public static void logout(Context context) {
        SharedUtil.getInstance(context).removeShared(Constant.USER_DATA);
    }

    public static boolean checkLogin(Context context) {
        String userData = SharedUtil.getInstance(context).readShared(Constant.USER_DATA,null);
        return !TextUtils.isEmpty(userData);
    }

    public static User getNowUser(Context context) {
        String userData = SharedUtil.getInstance(context).readShared(Constant.USER_DATA,null);
        if(TextUtils.isEmpty(userData)) {
            return null;
        }
        return JSON.parseObject(userData,User.class);
    }
}
